/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Panel;

import Controller.MyModel;
import DAOs.ReforestationZoneJpaController;
import DAOs.exceptions.NonexistentEntityException;
import Entity.ReforestationZone;
import Interface.Windows.ComparativeInterface;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author USUARIO
 */
public class ZonesListPanel extends javax.swing.JPanel {

    /**
     * Creates new form ZonesListPanel
     */
    
    private ReforestationZoneJpaController zonesJPA;
    private EntityManagerFactory emf;
    private MyModel zonesModel;
    private JTable zonesTable;
    private MouseListener mouse;
    private int deleteRow;
    private List<ReforestationZone> zone;
    
    public ZonesListPanel(EntityManagerFactory emf) {
        this.emf = emf;
        zonesJPA = new ReforestationZoneJpaController(this.emf);
        initComponents();
        zonesModel = getMyModel();
        zone = zonesJPA.findReforestationZoneEntities();
        for(ReforestationZone z : zone){
            agregarFila(z);
        }
        zonesTable = new JTable(zonesModel);
        createBTN.setEnabled(false);
        mouse = new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                deleteRow = zonesTable.rowAtPoint(e.getPoint());
                createBTN.setEnabled(true);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        };
        zonesTable.addMouseListener(mouse);
        tableScrollPanel.setViewportView(zonesTable);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel20 = new javax.swing.JPanel();
        logoLabel19 = new javax.swing.JLabel();
        titleLabel19 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        TFCoorX = new javax.swing.JTextField();
        TFCoorY = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        findBTN = new javax.swing.JButton();
        tableScrollPanel = new javax.swing.JScrollPane();
        TFZoom = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        eraseBTN = new javax.swing.JButton();
        createBTN = new javax.swing.JButton();

        logoLabel19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/searchPeasant.png"))); // NOI18N

        titleLabel19.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel19.setText("Buscar Zonas");

        javax.swing.GroupLayout jPanel20Layout = new javax.swing.GroupLayout(jPanel20);
        jPanel20.setLayout(jPanel20Layout);
        jPanel20Layout.setHorizontalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel19)
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jPanel20Layout.setVerticalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Buscar Zona: ");

        jLabel2.setText("CoorX:");

        jLabel3.setText("CoorY:");

        findBTN.setText("Buscar");
        findBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findBTNActionPerformed(evt);
            }
        });

        jLabel4.setText("Zoom:");

        eraseBTN.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Cancelar.png"))); // NOI18N
        eraseBTN.setText("Borrar");
        eraseBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eraseBTNActionPerformed(evt);
            }
        });

        createBTN.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/visit.png"))); // NOI18N
        createBTN.setText("Crear Visita");
        createBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createBTNActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TFCoorX)
                            .addComponent(TFCoorY)
                            .addComponent(TFZoom, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(findBTN))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tableScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(createBTN)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(eraseBTN, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TFCoorX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TFCoorY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TFZoom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(findBTN))
                .addGap(18, 18, 18)
                .addComponent(tableScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(createBTN, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(eraseBTN, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jPanel20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void findBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findBTNActionPerformed
        if(isDouble()){
        }
    }//GEN-LAST:event_findBTNActionPerformed

    private void eraseBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eraseBTNActionPerformed
        Long id = zone.get(deleteRow).getId();
        try {
            zonesJPA.destroy(id);
            zonesModel = getMyModel();
            zone = zonesJPA.findReforestationZoneEntities();
            for(ReforestationZone z : zone){
                agregarFila(z);
            }
            zonesTable = new JTable(zonesModel);
            zonesTable.addMouseListener(mouse);
            tableScrollPanel.setViewportView(zonesTable);
            JOptionPane.showMessageDialog(new JFrame(), "La Zona se ha borrado Correctamente", "Succes", JOptionPane.INFORMATION_MESSAGE);
        } catch (NonexistentEntityException ex) {
            JOptionPane.showMessageDialog(new JFrame(), "No se pudo borrar la zona", "Error", JOptionPane.ERROR_MESSAGE);
        }
        createBTN.setEnabled(false);
    }//GEN-LAST:event_eraseBTNActionPerformed

    private void createBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBTNActionPerformed
        ComparativeInterface ci = new ComparativeInterface(emf, zone.get(deleteRow));
        ci.setVisible(true);
    }//GEN-LAST:event_createBTNActionPerformed
    
    private boolean isDouble(){
        boolean isdouble = true;
        try{
            Double.parseDouble(TFCoorX.getText());
            Double.parseDouble(TFCoorY.getText());
            Double.parseDouble(TFZoom.getText());
        }catch(NumberFormatException ex){
            isdouble = false;
            JOptionPane.showMessageDialog(new JFrame(), "Por favor ingrese números válidos", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return isdouble;
    }
    
    private MyModel getMyModel(){
        MyModel myModel = new MyModel();
        myModel.addColumn("ID");
        myModel.addColumn("Coor X");
        myModel.addColumn("Coor Y");
        myModel.addColumn("Zoom");
        myModel.addColumn("Campesino NIT");
        return myModel;
    }
    
    private void agregarFila(ReforestationZone zone){
        Object[] obj = new Object[5];
        obj[0] = zone.getId();
        obj[1] = zone.getLatitude();
        obj[2] = zone.getLongitude();
        obj[3] = zone.getZoom();
        if(zone.getPeasant() != null){
            obj[4] = zone.getPeasant().getCedula();
        }else{
            obj[4] = new Integer(-1);
        }
        zonesModel.addRow(obj);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField TFCoorX;
    private javax.swing.JTextField TFCoorY;
    private javax.swing.JTextField TFZoom;
    private javax.swing.JButton createBTN;
    private javax.swing.JButton eraseBTN;
    private javax.swing.JButton findBTN;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JLabel logoLabel19;
    private javax.swing.JScrollPane tableScrollPanel;
    private javax.swing.JLabel titleLabel19;
    // End of variables declaration//GEN-END:variables
}
